当前位置:网站首页>已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
2022-07-03 12:34:00 【无 羡ღ】
成功解决:TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
报错代码
我的代码:
import requests
import time
import random
from lxml import etree
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"}
def baidu_search(search_keyword, max_page):
for page in range(max_page):
time.sleep(random.randint(1, 2))
url = 'https://www.baidu.com/s?wd=%s' % search_keyword + '&pn=' + str(page * 10)
r = requests.get(url, headers=headers)
html = r.text
print(html)
html_data = etree.HTML(html, etree.HTMLParser)
# r1 = html_data.xpath('//h3')
# r2 = html_data.xpath('//*[@class="c-abstract"]')
# r3 = html_data.xpath('//*[@class="t"]/a/@href')
#
# for i in range(8):
# r11 = r1[i].xpath('string(.)')
# r22 = r2[i].xpath('string(.)')
# r33 = r3[i]
# print(r11)
def main():
rows = [('吾皇万睡'), ('Nike')]
max_page = 1
for search_keyword in rows:
baidu_search(search_keyword, max_page)
if __name__ == '__main__':
main()
报错内容:
Traceback (most recent call last):
File "E:/Python/3.py", line 54, in <module>
main()
File "E:/Python/3.py", line 46, in main
baidu_search(search_keyword, max_page)
File "E:/Python/3.py", line 29, in baidu_search
html_data = etree.HTML(html,etree.HTMLParser)
TypeError: Argument 'parser' has incorrect type (expected lxml.etree._BaseParser, got type)

报错原因
报错内容翻译:TypeError:参数“parser”的类型不正确(应为lxml.etree。\u BaseParser,get type)
解决方法
把解析代码换成即可:
html_data = etree.HTML(html)
边栏推荐
- [combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
- C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
- 剑指 Offer 14- I. 剪绳子
- Sword finger offer 11 Rotate the minimum number of the array
- February 14, 2022, incluxdb survey - mind map
- Powerful avatar making artifact wechat applet
- When we are doing flow batch integration, what are we doing?
- Logback 日志框架
- [network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)
- C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
猜你喜欢

有限状态机FSM

Glide question you cannot start a load for a destroyed activity

01 three solutions to knapsack problem (greedy dynamic programming branch gauge)

对业务的一些思考

2022-02-14 incluxdb cluster write data writetoshard parsing

The 35 required questions in MySQL interview are illustrated, which is too easy to understand

Huffman coding experiment report

Brief introduction to mvcc

IDEA 全文搜索快捷键Ctr+Shift+F失效问题

如何在微信小程序中获取用户位置?
随机推荐
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
Differences and connections between final and static
【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
关于CPU缓冲行的理解
Brief introduction to mvcc
Logback 日志框架
Kotlin notes - popular knowledge points asterisk (*)
The latest version of lottery blind box operation version
My creation anniversary: the fifth anniversary
Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication
Mysqlbetween implementation selects the data range between two values
2022-02-14 incluxdb cluster write data writetoshard parsing
Grid connection - Analysis of low voltage ride through and island coexistence
【Colab】【使用外部数据的7种方法】
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
How to get user location in wechat applet?
2022-01-27 redis cluster technology research
2022-01-27 redis cluster cluster proxy predixy analysis
SSH login server sends a reminder
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous