#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()

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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With