'TypeError: a bytes-like object is required, not 'dict'

#coding:utf-8
import requests
from bs4 import BeautifulSoup

url = 'http://news.qq.com/'
wbdata = requests.get(url).text
soup = BeautifulSoup(wbdata,'lxml')
news_title = soup.select("div.text > em.f14 > a.linkto")

for n in news_title:
    title = n.get_text()
    link = n.get("href")
    data = {"标题":title,"链接":link}
    print(data)
    f = open('news.txt','wb')
    f.write(data)
    f.close()

enter image description here

Here are codes. So when I run it,it gives"TypeError: a bytes-like object is required, not 'dict'",I tried many solutions,no help. Can someone help me? thx!



Solution 1:[1]

f.write(data)

This is where the problem is. You are passing in a dictionary instead of a byte like object. For example when I change your code to the following:

#coding:utf-8
import requests
from bs4 import BeautifulSoup

url = 'http://news.qq.com/'
wbdata = requests.get(url).text
soup = BeautifulSoup(wbdata,'lxml')
news_title = soup.select("div.text > em.f14 > a.linkto")

for n in news_title:
    title = n.get_text()
    link = n.get("href")
    data = {"k":title,"a":link}
    print(data)
    f = open('news.txt','wb')
    data = b'123'
    f.write(data)
    f.close()

... I get the following:

{'k': '????????“??”?????????', 'a': 'http://news.qq.com/a/20170104/031454.htm'} ...

Which I assume is what you want.

Alternatively change the line:

f = open('news.txt', 'wb') 

to

f = open('news.txt', 'w')

and that way you can write in str rather than a byte-like object. In any case you shouldn't be passing in a dict.

Solution 2:[2]

Maybe you should open the file before write the title and the link,when you write end close the file .

f = open('news.txt','wb')
for n in news_titles:
    title = n.get_text()
    link = n.get("href")
    data= {
        '??':title,
        '??':link
    }
    f.write(data['??'])
    f.write(':')
    f.write(data['??'])
    f.write('\r\n')
f.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2