When writing Python crawlers, in order to save time, you usually open F12 in the browser and right-click to copy XPath.
There is a hole in Google browser, which took half a day
The copied XPath is as follows in
Python:// * [@ id = “mainframe”]/div/table/tbody/TR/TD [1]// text ()
Use the browser plug-in XPath helper to test the match successfully!
In the Python code, you can’t match it, as follows
xxx.xpath('//*[@id="mainFrame"]/div/table/tbody/tr/td[1]//text()')
The matching result is an empty list.
reason:
The browser “optimizes” the XPath, so that the XPath copied directly from the browser can’t be run in Python.
Solution:
Delete the extra tbody. The code is as follows:
#There is an extra tbody, delete it
xxx.xpath('//*[@id="mainFrame"]/div/table/tbody/tr/td[1]//text()')
# The modified code is as follows and successfully matches.
xxx.xpath('//*[@id="mainFrame"]/div/table/tr/td[1]//text()')
Read More:
- [Solved] Python Selenium Error: AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘
- Xpath Error: IndexError: list index out of range [How to Solve]
- [Solved] ParserError: NULL byte detected. This byte cannot be processed in Python‘s native csv library
- NameError: name ‘null’ is not defined [How to Solve]
- Opencv-python Install is Stuck Error: running setup.py bdist_wheel for opencv-python
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- [Solved] ERROR: No matching distribution found for torch-cluster==x.x.x
- Python Selenium: element is not attached to the page document error
- Python error: ‘int’ object is not callable
- [Solved] python Error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.
- RuntimeError: Failed to register operator torchvision::_new_empty_tensor_op. +torch&torchversion Version Matching
- Python environment error, bad interpreter: there is no file or directory
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- The automatic token of Python interface is passed into the header
- [Solved] ufunc ‘add‘ did not contain a loop with signature matching types (dtype(‘<U32‘), dtype(‘<U32‘))
- Python parsing JSON Error: NameError: name ‘false’ is not defined
- [Solved] Python serializate error: NameError: name ‘JSON’ is not defined
- [How to Fix]RuntimeError: Python is not installed as a framework, If you are using (Ana)Conda